import {messages} from "share/common";
import React, { PropTypes } from 'react';
import BUSINESS_MAP from '../service/business-service';
import INVOICE_MAP from '../service/invoice-service';

export default (WrappedComponent) => {
  class AttachmentHeaderHoc extends React.Component {
    wordsHandle = {
      invoice: INVOICE_MAP,
      business: BUSINESS_MAP,
      attachment: {
        headerTitle: {
          name: messages('expense-1.key668')/*附件名称*/,
          render: (value, record) => record.fileName
        },
        totalAmount: {},
        origin: {}

      }
    };

    state = {
      headerTitle: {},
      bodyWords: [], // body
      origin: {}, // 来源
      labels: [],
      totalAmount: {},
      descAmount: {},
      invoicedReceiptAmount: {}
    };

    componentDidMount() {
      this.init(this.props);
    }

    componentWillReceiveProps(nextProps){
      this.init(nextProps);
    };

    handleShowMore = () => {
      const { currentAdjunct, flag } = this.props;
      this.props.handleJumpRead(currentAdjunct, flag);
    };

    init(props) {
      const { flag, currentAdjunct: { receiptLabels = [] }, isShowUnUsedAmount } = props;
      const handler = this.wordsHandle[flag];
      let { headerTitle, origin, totalAmount, invoicedReceiptAmount = {}, unUsedAmount } = handler,
        descAmount = {},
        bodyWords = [],
        labels = [];
      if (flag === 'invoice') {
        descAmount = {
          render: (value, record, changes) => {
            const { fee, feeWithoutTax, taxString, nonDeductibleAmount } = record;
            return Object.keys({ fee, feeWithoutTax, taxString, nonDeductibleAmount })
              .map(item =>
                INVOICE_MAP[item].isHide && INVOICE_MAP[item].isHide(record[item])
                  ? null
                  : <span>{item === 'feeWithoutTax' ? '（' : ''} {INVOICE_MAP[item].name}: {INVOICE_MAP[item].render(record[item], record, changes[item])} {item === 'feeWithoutTax' ? '，' : ''} {item === 'taxString' ? ')' : ''}</span>
              );
          }
        };
        bodyWords = ['billingTime', 'billingCode', 'billingNo', 'payee', 'title', 'goodsName'].map(item => {
          return {
            ...INVOICE_MAP[item],
            dataIndex: item
          };
        });
        labels = receiptLabels.filter(item => item.type !== 'HAND_RECODE_RECEIPT');
      } else if (flag === 'business') {
        descAmount = {
          render: (value, record) => {
            const { posCurAmt, oriCurAmt } = record;
            return Object.keys({ posCurAmt, oriCurAmt })
              .map(item => <span>{BUSINESS_MAP[item].name}：{BUSINESS_MAP[item].render(record[item], record)}{item === 'posCurAmt' ? '，' : ''} </span>);
          }
        };
        bodyWords = ['trsDate', 'crdNum', 'bilMon', 'trsCod', 'remark'].map(item => {
          return {
            ...BUSINESS_MAP[item],
            dataIndex: item
          };
        });
      }
      this.setState({
        headerTitle,
        origin,
        descAmount,
        bodyWords,
        labels,
        totalAmount,
        invoicedReceiptAmount: isShowUnUsedAmount ? unUsedAmount : invoicedReceiptAmount
      });
    }

    render() {
      const { headerTitle, bodyWords, origin, labels, totalAmount, descAmount, invoicedReceiptAmount } = this.state;
      return (
        <WrappedComponent
          origin={origin}
          labels={labels}
          title={headerTitle}
          bodyWords={bodyWords}
          descAmount={descAmount}
          totalAmount={totalAmount}
          handleShowMore={this.handleShowMore}
          invoicedReceiptAmount={invoicedReceiptAmount}
          {...this.props}
        />
      );
    }
  }

  return AttachmentHeaderHoc;
}
